<script type="text/javascript">
var $datatable;
var _DATATABLE_TR;
var oTable;

var _IMPORT_DATE_FROM = '';
var _IMPORT_DATE_TO = '';
    
$(document).ready(function() {
    $datatable = $('#datatable_dvt');
    $dialog_form_edit = $('#dialog_form_edit');
    $dialog_form_add = $('#dialog_form_add');
    $dialog_confrim_delete = $('#dialog_confrim_delete');
    
    oTable = $datatable.dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bInfo": false,
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": false,
        "bFilter" : false,
        "bLengthChange": false,
        //"bFilter": false,
        "sAjaxSource": $datatable.attr('active_url'),
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "filter_date_from", "value": _IMPORT_DATE_FROM } );
            aoData.push( { "name": "filter_date_to", "value": _IMPORT_DATE_TO } );
            
            $.getJSON( sSource, aoData, function (json) { 
                fnCallback(json)
            } );

        },
        "fnDrawCallback": function( oSettings ) {
            //$("#datatable_nb_wrapper").find(".fg-toolbar").removeClass("ui-widget-header");
            datatable_selected();
            _DATATABLE_TR = null;
        },
        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if( aData['id'] != undefined ){
                $(nRow).attr("value_id",aData['id']);
            }
            if( aData['modify'] != undefined ){
                $(nRow).attr("modify",aData['modify']);
            }
            //if ( aData[4] == "A" ){$('td:eq(4)', nRow).html( '<b>A</b>' );}
            return nRow;
        }
        //"oTableTools": {"aButtons": ["copy", "csv", "xls", "pdf"]}
        
    });
    
    $(function() {
        $( "#tabs" ).tabs();
    });
    $(function() {
        $( ".datepicker" ).datepicker({
            dateFormat: "yy-mm-dd",
            showOn: "button",
            buttonImage: "<?php print base_url('template/ess/images/calendar_icon.gif'); ?>",
            buttonImageOnly: true,
            changeMonth: true,
            changeYear: true
        });
    });
    
    /*dialogFormAddNew();
    dialogConfrimDelete();
    dialogFormEdit();
    dialogError();*/
} );

function datatable_selected(){
    $datatable.find('tbody tr[modify=edit]').click( function( e ) {
        if ( $(this).hasClass('row_selected') ) {
            $(this).removeClass('row_selected');
            _DATATABLE_TR = null;
        }else {
            oTable.$('tr.row_selected').removeClass('row_selected');
            $(this).addClass('row_selected');
            _DATATABLE_TR = $(this).attr('value_id');
        }
    });
    
    $datatable.find('tbody tr[modify=edit]').bind("dblclick", function(){
        if ( !$(this).hasClass('row_selected') ) {
            $(this).addClass('row_selected');
        }
        _DATATABLE_TR = $(this).attr('value_id');
        even_edit_dbclick( _DATATABLE_TR );

    });

}

function even_edit_dbclick(id){
    var url = $dialog_form_edit.attr('active_url') + '/' + id;
        
    $.get(url, function(data) {
        $dialog_form_edit.html( data );
        $dialog_form_edit.dialog( "open" );
    });
}
/*
function dialogFormAddNew(){
    
    $dialog_form_add.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'insert'){
                            //oTable.fnDraw();
                            //oTable.fnFilter( data.ten );
                            oTable.fnSort( [ [0,'desc'] ] );
                        }
                        $this.dialog( "close" );
                    }
                });
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_add.html('');
        }
    });
        
}

function dialogFormEdit(){
    
    $dialog_form_edit.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "<?php print lang('btn_save') ?>": function() {
                var $this = $( this );
                $("#frm_add").ajaxSubmit({
                    //beforeSubmit: validate
                    dataType:  'json',
                    beforeSubmit: function(formData, jqForm, options) {                        
                        return validate_frm();
                    },
                    success: function(data) {
                        if(data.action == 'update'){
                            var $tr = oTable.$('tr[value_id="'+ data.id +'"]');
                            $tr.find('td:eq(1)').html(data.name);
                            $tr.find('td:eq(2)').html(data.status);
                        }
                        $this.dialog( "close" );
                    }
                });
            },
            "<?php print lang('btn_cancel') ?>": function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            $dialog_form_edit.html('');
        }
    });
        
}

function validate_frm(){
    var flag = true;
    var name = $('input[name=name]').val();
    if(name == ''){
        $("input[name=name]").addClass("i-error");
        $("#iname").show();
        flag = false;
    }else{
        $("#iname").hide();
        $("input[name=name]").removeClass("i-error");
    }
    
    return flag;
}

function dialogConfrimDelete(){
    
    $dialog_confrim_delete.dialog({
        autoOpen: false,
        //height: 300,
        width: 320,
        modal: true,
        resizable: true,
        buttons: {
            "Delete": function() {
                var url = $dialog_confrim_delete.attr('active_url');
                
                $.post(url, { id: _DATATABLE_TR },function(data){
                     _DATATABLE_TR = null;
                     oTable.fnDraw();
                }, "json");
                
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            //allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
        
}

function even_add(){
    var url = $dialog_form_add.attr('active_url');
    $.get(url, function(data) {
        $dialog_form_add.html( data );
        $dialog_form_add.dialog( "open" );
    });
}

function even_edit(){
    if(_DATATABLE_TR != null){
        var url = $dialog_form_edit.attr('active_url') + '/' + _DATATABLE_TR;
        $.get(url, function(data) {
            $dialog_form_edit.html( data );
            $dialog_form_edit.dialog( "open" );
        });
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}

function even_delete(){
    if(_DATATABLE_TR != null){
        $dialog_confrim_delete.dialog( "open" );
    }else{
        $dialog_error.html( '<?php print lang('error_noselected'); ?>' );
        $dialog_error.dialog( "open" );
    }
}
*/

function filter_datatable_import(){
    _IMPORT_DATE_FROM = $('input#import_date_from').val();
    _IMPORT_DATE_TO = $('input#import_date_to').val();
    oTable.fnDraw();
}
</script>


<div class="page-title 1ui-widget-header">
    <div class="title">Don Vi Tinh</div>
</div>

<div class="menu-button">
    <div class="message"></div>
    <!--<button onclick="even_add();"><?php print lang('btn_new'); ?></button>
    <button onclick="even_edit();"><?php print lang('btn_edit'); ?></button>
    <button onclick="even_delete();"><?php print lang('btn_delete'); ?></button>-->
    <div class="clear"></div>
</div>

<div class="warpp">

    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">Nhap Kho</a></li>
            <li><a href="#tabs-2">Xuat Kho</a></li>
            <li><a href="#tabs-3">Tra Ve</a></li>
        </ul>
        <div id="tabs-1">
            <div>
                <p>Tu Ngay: <input type="text" name="import_date_from" id="import_date_from" class="datepicker" />
                Den Ngay: <input type="text" name="import_date_to" id="import_date_to" class="datepicker" />
                <button onclick="filter_datatable_import()";>Filter</button>
                </p>
            </div>
            <div>
                <table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable_dvt" active_url="<?php print site_url('nk/datatable_filter_bill_import/'); ?>">
                    <thead>
                        <tr>
                            <th width="40px"><?php print lang('vt_stt'); ?></th>
                            <th width="100px"><?php print lang('vt_billcode'); ?></th>
                            <th width="200px"><?php print lang('vt_nguoinhap'); ?></th>
                            <th><?php print lang('vt_nhacungcap'); ?></th>
                            <th><?php print lang('vt_ngaynhap'); ?></th>
                            <th><?php print lang('vt_thanhtien'); ?></th>
                            <th><?php print lang('vt_note'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td colspan="5" class="dataTables_empty">Loading data from server</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div id="tabs-2">
            <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
        </div>
        <div id="tabs-3">
            <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
            <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
        </div>
    </div>
    
    
</div>

<div id="dialog_form_add" title="<?php print lang('vt_title_themdvt'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_dvt/'); ?>"></div>
<div id="dialog_form_edit" title="<?php print lang('vt_title_suadvt'); ?>" active_url="<?php print site_url('vt/ajax_loadfrm_dvt/'); ?>"></div>
<div id="dialog_confrim_delete" title="<?php print lang('title_thongbao'); ?>" active_url="<?php print site_url('vt/delete_dvt/'); ?>">
    <p><?php print lang('msg_delete'); ?></p>
</div>